package cn.senjoeson.demo.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.senjoeson.demo.bean.User;
import cn.senjoeson.demo.dao.UserDao;
import cn.senjoeson.demo.utils.MessageUtils;

public class Login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.print("访问已经到达");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if ("".equals(username) || "".equals(password)) {
            MessageUtils.sendMessage("index.jsp",request, response, "用户名或密码不能为空");
            return;
        } else {
            //下一步进行数据库操作
            UserDao userDao = new UserDao();
            User user = userDao.findByName(username);


            if (!username.equals(user.getUsername())) {     //用户名不正确
                MessageUtils.sendMessage("index.jsp",request, response, "用户名不存在");
            } else if (!password.equals(user.getPassword())) {      //密码不正确
                MessageUtils.sendMessage("index.jsp",request, response, "密码输入错误");
            } else {
                MessageUtils.sendMessage("index.jsp",request, response, "恭喜您,登陆成功");
                //response.sendRedirect();
            }

        }
    }


}
